package com.china08.yxyapi.api.home.kecheng.cloud;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;

/**
 * 
 * @annotation: 云课程--查看课程观看人数加一
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class KechengCloudStudyVvUpdController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsCloudLessonRepository yxyLessonsCloudLessonRepository;

    // @Autowired
    // private YxyLessonsUserStudyRepository yxyLessonsUserStudyRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/cloud/study/vv/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        // String userId = getProfile(request, response).getId();

        YxyLessonsCloudLesson cloudLesson = yxyLessonsCloudLessonRepository.findById(reqModel.lessonId);
        if (cloudLesson.getClickPv() == null) {
            cloudLesson.setClickPv(1);
            yxyLessonsCloudLessonRepository.save(cloudLesson);
        } else {
            cloudLesson.setClickPv(cloudLesson.getClickPv() + 1);
            yxyLessonsCloudLessonRepository.save(cloudLesson);
        }

        // YxyLessonsUserStudy userStudy =
        // yxyLessonsUserStudyRepository.findByUserIdAndLessonsId(userId,
        // reqModel.lessonId);
        // if (userStudy == null) {
        // YxyLessonsCloudLesson cloudLesson =
        // yxyLessonsCloudLessonRepository.findById(reqModel.lessonId);
        // cloudLesson.setVv(cloudLesson.getVv() + 1);
        // yxyLessonsCloudLessonRepository.save(cloudLesson);
        //
        // YxyLessonsUserStudy study = new YxyLessonsUserStudy();
        // study.setLessonsId(reqModel.lessonId);
        // study.setUserId(userId);
        // String id = userId + "_" + reqModel.lessonId;
        // study.setId(id);
        // yxyLessonsUserStudyRepository.save(study);
        // }
        RespModel model = new RespModel();

        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 课程ID
        public String lessonId;
    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
